//
//  SKYCPUFrequency.m
//  SKYCPUFrequency
//
//  Created by 张二帅 on 2018/5/25.
//  Copyright © 2018年 Skyon. All rights reserved.
//

#import "SKYCPUFrequency.h"


extern int sky_freqTest(int cycles);

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-function"
double SKYGetCPUFrequency(void)
{
    volatile NSTimeInterval times[500];
    
    int sum = 0;
    
    for(int i = 0; i < 500; i++)
    {
        times[i] = [[NSProcessInfo processInfo] systemUptime];
        sum += sky_freqTest(10000);
        times[i] = [[NSProcessInfo processInfo] systemUptime] - times[i];
    }
    
    NSTimeInterval time = times[0];
    for(int i = 1; i < 500; i++)
    {
        if(time > times[i])
            time = times[i];
    }
    
    double freq = 1300000.0 / time;
    return freq;
}
#pragma clang diagnostic pop

@implementation SKYCPUFrequency

+ (NSString *)cpuFrequencyKHZ {
    return [NSString stringWithFormat:@"%luKHZ",(unsigned long)([self cpuFrequency]/1000)];
}

+ (double)cpuFrequency {
    double cpuFrequency = SKYGetCPUFrequency();
    return cpuFrequency;
}

@end
